﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows;
using System.Threading;
using System.Windows.Threading;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;

namespace TakeNote
{





    public partial class Window2 : Window
    {

        public const int SWP_HIDEWINDOW = 128;
        public const int SWP_SHOWWINDOW = 64;
        private const uint LBUTTONDOWN = 0x00000002; // 왼쪽 마우스 버튼 누름
        private const uint LBUTTONUP = 0x00000004; // 왼쪽 마우스 버튼 땜
       
        public const int WM_MOUSEMOVE = 0x200;
        public const int WM_LBUTTONDOWN = 0x201;
        public const int WM_LBUTTONUP = 0x202;
        public const int WM_LBUTTONDBLCLK = 0x203;
        private Thread m_ThreadListen = null;
        bool isDrag = false;
        System.Drawing.Point previousPoint;
        System.Windows.Shapes.Line rr;
      
        System.Windows.Shapes.Line rrr;

        //System.Windows.Shapes.Line rrrr = new System.Windows.Shapes.Line();
        public static byte alapha = 0xff;
        public static double width;
        public static double height;
        public static double padding;
        int setTool = 0;
        int setStrip = 0;
        ColorDialog clg;
        int setColor = 0;
        int xx=0;
        int yy = 0;
        int xxx;
        int yyy;
        
      

           
            //    while (true)
           // {
                
          //  } 

        private void 굵기10ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTool = 1;
        }

        private void 굵기5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTool = 2;
        }

        private void 굵기3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTool = 3;
        }

        private void 굵기1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setTool = 0;
        }

        private void dotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setStrip = 1;
        }

        private void dashDotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setStrip = 2;
        }

        private void soildToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setStrip = 0;
        }

        private void 선LToolStripMenuItem_Click(object sender, EventArgs e)
        {
            clg = new ColorDialog();
            clg.ShowDialog();
            setColor = 1;
            // Set the initial color of the dialog to the current text color

        }
        public void ChangePenColor()
        {
            if (Server.Colorflag == 0)
            {
                ChangePenColor(System.Windows.Media.Color.FromArgb(alapha, 0, 0, 0)); 
               // rrr.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black);
            }
            else if (Server.Colorflag == 1)
            {
                ChangePenColor(System.Windows.Media.Color.FromArgb(alapha, 255, 0, 0));
                //rrr.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
            }
            else if (Server.Colorflag == 2)
            {
                ChangePenColor(System.Windows.Media.Color.FromArgb(alapha, 0, 0, 255));
               // rrr.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent);
            }
          
        }
        public void ChangeAlapha_true()
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
            {
            alapha = 0x64;//형광펜용 투명도 조절
            this.canvas1.DefaultDrawingAttributes.Width = 15;
            this.canvas1.DefaultDrawingAttributes.Height = 15;
             }));

        }
        public void ChangeAlapha_false()
        { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
            {
            alapha = 0xFF; //불투명 _ 일반펜용
            this.canvas1.DefaultDrawingAttributes.Width = 3;
            this.canvas1.DefaultDrawingAttributes.Height = 3;
            }));

        }
        private void ChangePenColor(System.Windows.Media.Color color)
        {
            this.canvas1.DefaultDrawingAttributes.Color = color;


        }

         public void ink_draw()
        {
       
            Console.WriteLine("dfsdf");
 
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
            {
                Console.WriteLine("X: " +Server.X);
                Console.WriteLine("Y : " + Server.Y);
                Console.WriteLine("preX : " + Server.preX);
                Console.WriteLine("preY: " + Server.preY);
                
                    rrr = new System.Windows.Shapes.Line { X1 = Server.X, Y1 = Server.Y, X2 = Server.preX, Y2 = Server.preY };
                    ChangePenColor();
                
                    rrr.StrokeThickness = 1;
                    canvas1.Children.Add(rrr);

                }));

        }

         public void mouseup()
         {

             UserImportDLL.mouse_event(LBUTTONUP, 0, 0, 0, 0);
         }


        /// <summary>
        /// /////////////////
        /// </summary>
         public void ink_draw_test2()
         {
             ChangePenColor();
             System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)Server.Average_X + (int)padding , (int)Server.Average_Y);
            
             //System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)Server.X + (int)padding, (int)Server.Y);
            //System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)Server.X , (int)Server.Y);
             UserImportDLL.mouse_event(LBUTTONDOWN, 0, 0, 0, 0);


         }
        /// <summary>
        /// //////////////
        /// </summary>


        public void ink_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
           /// while (true)
         //   {
              //  Console.WriteLine("saddd");

              //  rr = new System.Windows.Shapes.Line { X1 = Server.X, Y1 = Server.Y, X2 = Server.preX, Y2 = Server.preY };
            //    rr.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
             //   rr.StrokeThickness = 1;
              //  inkCanvas.Children.Add(rr);
              //  break;
            //}

        }

 

        private void panel1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
           // Console.WriteLine("saddd");
            if (isDrag == true)
            {
                Console.WriteLine("흠");
                System.Drawing.Pen currentPen;
                   int xCooldinate = (int)e.GetPosition(this).X;
                   int yCooldinate = (int)e.GetPosition(this).Y;
                   Console.WriteLine(xCooldinate);
                System.Drawing.Point currentPoint = new System.Drawing.Point(xCooldinate, yCooldinate);
                if (setColor == 0)
                    currentPen = new System.Drawing.Pen(System.Drawing.Color.White);
                else
                    currentPen = new System.Drawing.Pen(clg.Color);

                if (setTool == 0)
                    currentPen.Width = 1;
                else if (setTool == 1)
                    currentPen.Width = 10;
                else if (setTool == 2)
                    currentPen.Width = 5;
                else if (setTool == 3)
                    currentPen.Width = 3;

                if (setStrip == 0)
                    currentPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                else if (setStrip == 1)
                    currentPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                else if (setStrip == 2)
                    currentPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

                Console.WriteLine("llllj");
              //  while (true)
               // {
             
                    Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
                    g.DrawLine(currentPen, xx, yy, xxx, yyy);
                    xx = xx + 10;
                    yy = yy + 10;
                    previousPoint = currentPoint;
                //}
            }
        }

 
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Console.WriteLine("paint");
        }
        public Window2()
        {
            InitializeComponent();
            this.Topmost = true;
            this.ShowInTaskbar = true;


            height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            double width1 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;  //전체 가로 크기
            width = height * 4 / 3; // 비율상 widh크기 구하기 
            padding = (width1 - width) / 2;
            this.Left = padding;
            this.Width = width;
            this.Height = height;
            this.Top = 0;
            Console.WriteLine("어디서 호출되는거지?");
            this.canvas1.DefaultDrawingAttributes.Width = 1;
            this.canvas1.DefaultDrawingAttributes.Height = 1;
          
            this.canvas1.DefaultDrawingAttributes.IgnorePressure = false;

         //   Graphics g = Graphics.FromHwnd(new System.Windows.Interop.WindowInteropHelper(this).Handle);
            // 선그리기
           // Pen pen = new Pen(Color.Green, 50); //pen 객체생성및 속성정의(선을 그리기위해 pen이 필요)
           // g.DrawLine(pen, 0, 0, 1000, 900);
           // Console.WriteLine(" width : " + width + " Height : " + height);
           




            
        //    rrr.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black);
          }          

        private void panel1_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Console.WriteLine("111ccc");
            isDrag = false;
        }
        public void inkCanvas_Clear()
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
           {
                canvas1.Strokes.Clear();
           }));
            

        }
        private void panel1_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Console.WriteLine("hhh");
            isDrag = true;
            int xCooldinate = (int)e.GetPosition(this).X;
            int yCooldinate = (int)e.GetPosition(this).Y;

            previousPoint = new System.Drawing.Point(xCooldinate, yCooldinate);
        }
        private void ShowWin()
        {
            int IRet;
            IRet = UserImportDLL.FindWindowA("Shell_traywnd", "");

            if (IRet > 0)
            {
                IRet = UserImportDLL.SetWindowPos(IRet, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
            }
        }

        private void HideWin()
        {
            int IRet;
            IRet = UserImportDLL.FindWindowA("Shell_traywnd", "");

            if (IRet > 0)
            {
                IRet = UserImportDLL.SetWindowPos(IRet, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
            }
        }
        private void panel1_Loaded(object sender, RoutedEventArgs e)
        {
            HideWin();
  
        }

        private void inkCanvas_Gesture(object sender, InkCanvasGestureEventArgs e)
        {

        }

        private void inkCanvas_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            
        }

        private void inkCanvas_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            canvas1.Strokes.Clear();
        }

        private void canvas1_Gesture(object sender, InkCanvasGestureEventArgs e)
        {

        }

        private void panel1_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("sdasdasdasd");
            ShowWin();

        }


   

      

       


   
    }
    public class UserImportDLL
    {
        [DllImport("User32.dll")]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, int dwData, int dwExtraInfo);
       [DllImport("User32.dll")]
        public static extern int FindWindowA(string IpClassName, string IpWindowName);
        [DllImport("User32.dll")]
        public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
    }
}
